package com.tanhua.dubbo.mongo.api;

import com.tanhua.model.mongo.Options;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class OptionsApiImpl implements OptionsApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据试题id获取答案集合
    public Map<ObjectId, List<Options>> findOptions(List<ObjectId> queIds) {

        Map<ObjectId, List<Options>> map=new HashMap<>();
        for (ObjectId queId : queIds) {

            Criteria criteria = Criteria.where("questionsId").is(queId);
            Query query = Query.query(criteria);
            List<Options> options = mongoTemplate.find(query, Options.class);
            map.put(queId,options);
        }
        return map;
    }

    //根据试题id和答案id获取答案的分数
    public int selectByOptionId(String questionId, String optionId) {

        Criteria criteria = Criteria.where("questionsId").is(new ObjectId(questionId))
                .and("id").is(new ObjectId(optionId));

        Query query = Query.query(criteria);

        Options option = mongoTemplate.findOne(query, Options.class);

        return option.getScore();
    }
}
